package com.hoshino.momochi.controller;

import com.hoshino.momochi.model.domain.SensitiveWord;
import com.hoshino.momochi.service.SensitiveWordService;
import com.hoshino.momochi.util.CodeEnum;
import com.hoshino.momochi.util.JsonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * 该类当中 所有操作 均限制 用户id 为 1 的用户操作
 */
@RestController
public class SensitiveWordController {

    @Autowired
    private SensitiveWordService sensitiveWordService;

    private final String USER_ID = CodeEnum.USERID.getMessage();

    /**
     * 添加敏感词
     *
     * @param request
     * @param sensitiveWord 需求字段 word
     * @return
     */
    @RequestMapping("api/sensitive/add")
    public JsonResponse addSensitiveWord(HttpServletRequest request, @RequestBody SensitiveWord sensitiveWord) {
        Long uid = (Long) request.getAttribute(USER_ID);
        if (sensitiveWord.getWord() != null && Objects.nonNull(uid) && uid.intValue() == 1) {
            sensitiveWordService.addSensitiveWord(sensitiveWord);
        }
        return JsonResponse.success(CodeEnum.SUCCESS);
    }

    /**
     * 批量添加敏感词
     *
     * @param request
     * @param sensitiveWord 需求字段 word_list
     * @return
     */
    @RequestMapping("api/sensitive/addList")
    public JsonResponse addSensitiveWordList(HttpServletRequest request, @RequestBody SensitiveWord sensitiveWord) {
        Long uid = (Long) request.getAttribute(USER_ID);
        if (Objects.isNull(uid) || uid.intValue() != 1) {
            return JsonResponse.success(CodeEnum.ERROR);
        }
        List<String> words = sensitiveWord.getWord_list();
        if (words != null) {
            sensitiveWordService.addSensitiveWords(words);
        }
        return JsonResponse.success(CodeEnum.SUCCESS);
    }

    /**
     * 删除 敏感词
     *
     * @param request
     * @param sensitiveWord
     * @return
     */
    @RequestMapping("api/sensitive/del")
    public JsonResponse delSensitiveWord(HttpServletRequest request, @RequestBody SensitiveWord sensitiveWord) {
        Long uid = (Long) request.getAttribute(USER_ID);
        if (Objects.isNull(uid) || uid.intValue() != 1) {
            return JsonResponse.success(CodeEnum.ERROR);
        }
        if (sensitiveWord.getWord() != null) {
            sensitiveWordService.delSensitiveWord(sensitiveWord);
        }
        return JsonResponse.success(CodeEnum.SUCCESS);
    }

}
